package com.gz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gz.entity.China;
import com.gz.service.ChinaService;
import com.gz.mapper.ChinaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* @author 龙龙与贝儿
* @description 针对表【t_china】的数据库操作Service实现
* @createDate 2025-05-22 10:29:58
*/
@Service
public class ChinaServiceImpl extends ServiceImpl<ChinaMapper, China>
    implements ChinaService{

    @Autowired
    private ChinaMapper chinaMapper;
    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public List<China> getAddress(Integer pid, String key) {
        Boolean b = redisTemplate.hasKey(key);
        if (b) {
            Set<China> set = redisTemplate.opsForZSet().range(key, 0, -1);
            List<China> list = new ArrayList<>();
            set.forEach(e -> {list.add(e);});
            return list;
        }else {
            LambdaQueryWrapper<China> wrapper = Wrappers
                    .lambdaQuery(China.class)
                    .eq(China::getPid, pid)
                    .ne(China::getId, 0)
                    .orderByAsc(China::getId);
            List<China> list = chinaMapper.selectList(wrapper);
            list.forEach(china -> {redisTemplate.opsForZSet().add(key, china, china.getId());});
            return list;
        }
    }

}




